﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WJ.Infrastructure.DDD;


namespace WJ.Infrastructure.EF
{
    public class RepositoryBase<TEntity> : IRepository<TEntity> where TEntity : class, IAggregateRoot
    {

        protected EFContext Dbcontext;
       
        public RepositoryBase()
        { 
            Dbcontext=IoC.Resolve<IUnitOfWork>() as EFContext;
        }

        public void Add(TEntity entity)
        {
            Dbcontext.Set<TEntity>().Add(entity);
        }

        public void Remove(TEntity entity)
        {
            Dbcontext.Set<TEntity>().Remove(entity);
        }

        public TEntity FindById(Guid id)
        {
            return Dbcontext.Set<TEntity>().Find(id);
        }


        public void Update(TEntity entity)
        {
            Dbcontext.Entry(entity).State = System.Data.Entity.EntityState.Modified;
        }

        public void UpSert(TEntity entity)
        {
           Dbcontext.Set<TEntity>().AddOrUpdate(entity);
        }

    }
}
